// app.js
import {
	Token
} from 'utils/token.js';

App({
	onLaunch() {
		wx.getSystemInfo({
			success: res => {
				if (res.model.search('iPhone X') != -1 || res.model.search('iPhone XS') != -1 || res
					.model.search('iPhone XS Max') != -1 || res.model.search('iPhone XR') != -1 ||
					res.model.search('unknown') != -1) {
					this.globalData.model.phone = 'iPhone X'
				} else if (res.model.search('iPhone') != -1) {
					this.globalData.model.phone = 'iPhone'
				} else {
					this.globalData.model.phone = res.model
				}
				var bottomHeight = res.screenHeight - res.safeArea.bottom;
				this.globalData.model.bottomHeight = bottomHeight > 0 ? bottomHeight : 13;
			}
		})
		const res = wx.getMenuButtonBoundingClientRect();
		this.globalData.model.topHeight = res.height;
		this.globalData.model.topMargin = res.top;

		var that = this;
		var token = new Token();
		token.verify((res) => {
			that.globalData.isLogin = 1;
			// 所以此处加入 callback 防止首页延迟
			if (that.checkLoginReadyCallback) {
				that.checkLoginReadyCallback(res);
			}
		});

		// this.checkToken();
	},


	// 判断是否为手机号
	isPhone(pone) {
		var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		if (!myreg.test(pone)) {
			return false;
		} else {
			return true;
		}
	},
	// 验证身份证号码格式
	validateIdNumber(idNumber) {
		const regex = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)$/;
		return regex.test(idNumber);
	},
	//防抖
	debounce(func, wait) {
		let timeout;
		return function() {
			const context = this;
			const args = arguments;
			clearTimeout(timeout);
			timeout = setTimeout(function() {
				func.apply(context, args);
			}, wait);
		};
	},

	globalData: {
		userInfo: null,
		model: {
			phone: '',
			topHeight: '',
			topMargin: '',
			bottomHeight: 0
		},
		isLogin: false,
	}
})